package restaurant;
import java.util.ArrayList;
import java.util.TreeSet;

public class MohamReport {
	// an Array list that will hold The Tableid
	private ArrayList<Integer> allocatedtables = new ArrayList<Integer>();
	private boolean hit = false;
	private boolean hitonce = true;
	private String output = "";
	private double Totalcost = 0;
	private double cost;

	

	// -------------------------------------------------------------------------------------------------------------
	/**
	 * This function accepts 2 lists , an array list or orderItem and a TreeSet
	 * of MenuItems The function will loop on the order Arraylist getting all
	 * orders for each table , the dishes , quantity and will calculate the
	 * price
	 * 
	 * @param treeSet
	 * @param orderList
	 * @return
	 */
	public String GetTablePrice(TreeSet<MenuItem> treeSet,
			ArrayList<OrderItem> orderList) {
		for (OrderItem pr : orderList) {
			for (OrderItem pr2 : orderList) {
				if (pr.getTableid() == pr2.getTableid()) {
					for (Integer I : allocatedtables) {
						if (I.equals(pr.getTableid())) {
							hit = true;
						}
					}
					if (hit == false) {
						if (hitonce == true) {
							if (Totalcost != 0)
								output += "Total Cost"
										+ String.format("%.2f", Totalcost)
										+ "\n";
							output += "********************************" + "\n";
							output += "Payment for Table Number"
									+ pr.getTableid() + "\n";
							hitonce = false;
							Totalcost = 0;
						}
						for (MenuItem MI : treeSet) {
							if (MI.getDish().contains(pr2.getName())) {
								cost = MI.getCost();
								break;
							}

						}
						output += pr2.getName()
								+ "   "
								+ pr2.getQuantity()
								+ "*"
								+ cost
								+ "="
								+ String.format("%.2f",
										cost * pr2.getQuantity()) + "\n" + "\n";
						Totalcost += cost * pr2.getQuantity();
					}
				}
			}
			hitonce = true;
			// will add the Tableid to the allocatedtables Arraylist so that
			// when don't get the dishes for that table again
			allocatedtables.add(pr.getTableid());
			hit = false;
		}
		output += "Total Cost" + String.format("%.2f", Totalcost) + "\n";
		output += "********************************" + "\n";
		System.out.print(output);
		return output;
	}

	// -------------------------------------------------------------------------------------------------------------
	/**
	 * This function will get all the dishes that are not orderd
	 * 
	 * @param treeSet
	 * @param orderList
	 * @return
	 */
	public String FoodNotOrderd(TreeSet<MenuItem> treeSet,
			ArrayList<OrderItem> orderList) {
		boolean hit = false;
		String output2 = "********Dishes Not Orderd*********" + "\n";
		for (MenuItem MI : treeSet) {
			for (OrderItem pr : orderList) {
				if (MI.getDish().equals(pr.getName())) {
					hit = true;
					break;

				}
			}
			if (hit == false) {
				output2 += MI.getDish() + "\n";

			}
			hit = false;
		}

		return output2;
	}

	// -------------------------------------------------------------------------------------------------------------
	/**
	 * this function will get the frequancy of food orderd
	 * 
	 * @param orderList
	 * @return
	 */
	public String DishFrequancy(ArrayList<OrderItem> orderList) {
		String output2 = "";
		boolean hit2 = false;
		ArrayList<String> allocatedDishes = new ArrayList<String>();
		int count = 0;
		for (OrderItem pr : orderList) {
			for (OrderItem pr2 : orderList) {
				if (pr.getName().equals(pr2.getName())) {
					// this loop will prevent the same dish from being added to
					for (String I : allocatedDishes) {
						if (I.equals(pr.getName()) == true) {
							hit = true;
							break;

						}

					}
					if (hit == false) {
						count++;
					}

				}
			}
			// this loop is used so i don't print the dish more than one time
			for (String I : allocatedDishes) {
				if (I.equals(pr.getName()) == true) {
					hit2 = true;

					break;

				}
			}
			if (hit2 == false) {
				output2 += pr.getName() + "   " + "number of time orderd is"
						+ "   " + count + "\n" + "--------------" + "\n";
			}

			count = 0;
			hit = false;
			hit2 = false;
			// after every iteration the dish is added to a array list which is
			// used to prevent the function from getting the frequancy for the
			// same dish more than once
			allocatedDishes.add(pr.getName());
		}
		System.out.print(output2);
		return output2;
	}
	// -------------------------------------------------------------------------------------------------------------
}